﻿
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using AppFramework.DinosaursParkMes.Production.DTO;
using AppFramework.DinosaursParkMes.WorkOrder;
using CommunityToolkit.HighPerformance.Helpers;
using Microsoft.EntityFrameworkCore;
using Stripe;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Text;
using System.Threading.Tasks;

namespace AppFramework.DinosaursParkMes.Production
{
    public class MaterialRequisitionService : AppFrameworkAppServiceBase, IMaterialRequisitionService
    {
        private readonly IRepository<MaterialRequisition> repository;
        private readonly IRepository<MaterialRequisitionBOM> _repository;
        private readonly IWorkOrderAppService workOrderApp;
        public MaterialRequisitionService(IRepository<MaterialRequisition> repository, IWorkOrderAppService workOrderApp = null, IRepository<MaterialRequisitionBOM> _repository=null)
        {
            this.repository = repository;
            this.workOrderApp = workOrderApp;
            this._repository = _repository;
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PagedResultDto<MaterialShowDTO>> GetMaterialShow(MaterialImportDTO dto)
        {
            PagedResultDto<MaterialShowDTO> Pages = new PagedResultDto<MaterialShowDTO>();
            var list = from x in repository.GetAll()
                       select new MaterialShowDTO
                       {
                           Materialrequisitionname=x.Materialrequisitionname,
                           Materialrequisitiondate=x.Materialrequisitiondate,
                           Person=x.Person,
                           Workordercode=x.Workordercode,
                           Taskcode=x.Taskcode,
                           Workordername=x.Workordername,
                           Taskname=x.Taskname,
                           Materialrequisitioncode=x.Materialrequisitioncode,
                           Processroute=x.Processroute,
                           Processname=x.Processname,
                           Remark=x.Remark,
                           Id=x.Id,
                           State=x.State,
                       };
            if(!string.IsNullOrWhiteSpace(dto.Materialrequisitionnamecode))
            {
                list = list.Where(x => x.Materialrequisitioncode == dto.Materialrequisitionnamecode || x.Materialrequisitionname == dto.Materialrequisitionnamecode);
            }
            if (!string.IsNullOrWhiteSpace(dto.Workordernamecode))
            {
                list = list.Where(x => x.Workordercode == dto.Materialrequisitionnamecode || x.Workordername == dto.Materialrequisitionnamecode);
            }
            
            if(dto.Materialrequisitiondate!=null)
            {
                list = list.Where(x => x.Materialrequisitiondate == dto.Materialrequisitiondate);
            }

            Pages.TotalCount=list.Count();
            Pages.Items= await list.OrderByDescending(x=>x.Id).Skip((dto.pageindex-1)*dto.pagesize).Take(dto.pagesize).ToListAsync();
            return Pages;
            //return new PagedResultDto<MaterialShowDTO>()
            //{
            //    TotalCount = list.Count(),
            //    Items = list.OrderByDescending(x => x.Id).Skip((dto.pageindex - 1) * dto.pagesize).Take(dto.pagesize).ToList(),
            //};
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> PostMaterAdd(MaterialRequisitionDTO dto)
        {
            //开启事务
            var x = UnitOfWorkManager.Begin();
            try
            {
               if(dto.Item!=null)
                {
                    var modelName = "MaterialRequisition";
                    var dtos = new MaterialRequisition()
                    {
                        Materialrequisitionname = dto.Materialrequisitionname,
                        Materialrequisitiondate = dto.Materialrequisitiondate,
                        Materialrequisitioncode = await workOrderApp.GetCode(modelName),
                        Person = dto.Person,
                        Workordername = dto.Workordername,
                        Workordercode = dto.Workordercode,
                        Taskname = dto.Taskname,
                        Taskcode = dto.Taskcode,
                        Processroute = dto.Processroute,
                        Processname = dto.Processname,
                        Remark = dto.Remark,
                    };
                    await repository.InsertAsync(dtos);
                    foreach (var item in dto.Item)
                    {
                        var dtos2 = new MaterialRequisitionBOM()
                        {
                            Serialnumber = item.Serialnumber,
                            Materialrequisitioncode = dtos.Materialrequisitioncode,
                            Materialname = item.Materialname,
                            Materialcode = item.Materialcode,
                            Specificationsandmodels = item.Specificationsandmodels,
                            Unit = item.Unit,
                            Stock = item.Stock,
                            Number = item.Number,
                            Batchnumber = item.Batchnumber,
                            Warehousename = item.Warehousename,
                            Storagelocationname = item.Storagelocationname,
                            Remark = item.Remark,
                            Reservoirareaname = item.Reservoirareaname
                        };
                        await _repository.InsertAsync(dtos2);
                    }
                }
                else
                {
                    var modelName = "MaterialRequisition";
                    var dtos = new MaterialRequisition()
                    {
                        Materialrequisitionname = dto.Materialrequisitionname,
                        Materialrequisitiondate = dto.Materialrequisitiondate,
                        Materialrequisitioncode = await workOrderApp.GetCode(modelName),
                        Person = dto.Person,
                        Workordername = dto.Workordername,
                        Workordercode = dto.Workordercode,
                        Taskname = dto.Taskname,
                        Taskcode = dto.Taskcode,
                        Processroute = dto.Processroute,
                        Processname = dto.Processname,
                        Remark = dto.Remark,
                    };
                    await repository.InsertAsync(dtos);
                }
                //提交事务
                x.Complete();
                return 1;
            }
            catch (Exception)
            {
                //回滚事务
                x.Dispose();
                throw;
            }
        }
    }
}
